package com.yfp.operation.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.poi.ExcelUtil;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.operation.domain.UserAccount;
import com.yfp.operation.domain.UserBill;
import com.yfp.operation.domain.UserWithdraw;
import com.yfp.operation.domain.dto.UserWithdrawCheckDTO;
import com.yfp.operation.domain.dto.UserWithdrawRequestDTO;
import com.yfp.operation.domain.dto.UserWithdrawResponseDTO;
import com.yfp.operation.domain.dto.UserWithdrawResponseInfoDTO;
import com.yfp.operation.service.UserAccountService;
import com.yfp.operation.service.UserBillService;
import com.yfp.operation.service.UserWithdrawService;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 用户提现账单
 * 
 * @CreateDate: 2024/5/28 15:27
 * @UpdateUser:
 * @UpdateDate: 2024/5/28 15:27
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@RestController
@RequestMapping("/withdraw")
@Tag(name = "用户提现账单")
@Slf4j
public class UserWithdrawController extends BaseController {

    @Resource
    private UserWithdrawService userWithdrawService;

    @Resource
    private UserAccountService userAccountService;

    @Resource
    private UserBillService userBillService;


    @Operation(summary = "我的-提现-额度统计接口（总额、已提、可提）", description = "投资人提现")
    @GetMapping("/summary")
    public R<UserAccount> getAccountSummary() {
        Long userId = SecurityUtils.getUserId();
        return R.ok(userAccountService.findByUserId(userId));
    }

    @Operation(summary = "我的-提现-提现接口", description = "投资人提现")
    @GetMapping()
    public R<Void> withdraw() {
        userWithdrawService.accountWithdraw();
        return R.ok();
    }

    @Operation(summary = "我的-提现-历史记录接口（带分页）", description = "投资人提现")
    @GetMapping("/history/list")
    public R<TableDataInfo<List<UserWithdraw>>> getAccountWithdraw() {
        startPage();
        TableDataInfo dataTable = getDataTable(userWithdrawService.getAccountWithdraw());
        return R.ok(dataTable);
    }

    @Operation(summary = "我的-提现-分账明细接口（带分页）", description = "投资人提现")
    @GetMapping("/bill/list")
    public R<TableDataInfo<List<UserBill>>> getAccountBill() {
        startPage();
        TableDataInfo dataTable = getDataTable(userBillService.getAccountBill());
        return R.ok(dataTable);
    }

    @Operation(summary = "PC后台管理-提现管理-列表接口", description = "PC后台管理")
    @GetMapping("/history/management/list")
    public R<TableDataInfo<List<UserWithdrawResponseDTO>>> getManagementWithdrawList(UserWithdrawRequestDTO requestDTO) {
        startPage();
        if (ObjectUtil.isNotNull(requestDTO.getStartTime())) {
            requestDTO.setStartTime(DateUtil.beginOfDay(requestDTO.getStartTime()));
        }
        if (ObjectUtil.isNotNull(requestDTO.getEndTime())) {
            requestDTO.setEndTime(DateUtil.endOfDay(requestDTO.getEndTime()));
        }
        TableDataInfo dataTable = getDataTable(userWithdrawService.getManagementWithdrawList(requestDTO));
        return R.ok(dataTable);
    }

    @Operation(summary = "PC后台管理-提现管理-列表导出接口", description = "PC后台管理")
    @PostMapping("/history/management/list/export")
    @Log(title = "分账-提现管理-列表导出", businessType = BusinessType.EXPORT)
    public void getManagementWithdrawListExport(HttpServletResponse response, UserWithdrawRequestDTO requestDTO) {
        if (ObjectUtil.isNotNull(requestDTO.getStartTime())) {
            requestDTO.setStartTime(DateUtil.beginOfDay(requestDTO.getStartTime()));
        }
        if (ObjectUtil.isNotNull(requestDTO.getEndTime())) {
            requestDTO.setEndTime(DateUtil.endOfDay(requestDTO.getEndTime()));
        }
        List<UserWithdrawResponseDTO> list = userWithdrawService.getManagementWithdrawList(requestDTO);
        ExcelUtil<UserWithdrawResponseDTO> util = new ExcelUtil<>(UserWithdrawResponseDTO.class);
        util.exportExcel(response, list, "提现数据");
    }

    @Operation(summary = "PC后台管理-提现管理-详情接口", description = "PC后台管理")
    @GetMapping("/history/management/info")
    public R<List<UserWithdrawResponseInfoDTO>> getManagementWithdrawInfo(@ApiParam(value = "用户提现账单id")@RequestParam("userWithdrawId") Long userWithdrawId) {
        return R.ok(userWithdrawService.getManagementWithdrawInfo(userWithdrawId));
    }

    @Operation(summary = "PC后台管理-提现管理-详情导出接口", description = "PC后台管理")
    @PostMapping("/history/management/info/export")
    @Log(title = "分账-提现管理-详情导出", businessType = BusinessType.EXPORT)
    public void getManagementWithdrawInfoExport(HttpServletResponse response, @ApiParam(value = "用户提现账单id")@RequestParam("userWithdrawId") Long userWithdrawId) {
        List<UserWithdrawResponseInfoDTO> list = userWithdrawService.getManagementWithdrawInfo(userWithdrawId);
        ExcelUtil<UserWithdrawResponseInfoDTO> util = new ExcelUtil<>(UserWithdrawResponseInfoDTO.class);
        util.exportExcel(response, list, "提现详情数据");
    }

    @Operation(summary = "PC后台管理-提现管理-审核接口", description = "PC后台管理")
    @PostMapping("/history/management/check")
    public R<Void> managementWithdrawCheck(@RequestBody UserWithdrawCheckDTO userWithdrawCheckDTO) throws WxPayException {
        userWithdrawCheckDTO.setAuditUserId(SecurityUtils.getUserId());
        userWithdrawService.managementWithdrawCheck(userWithdrawCheckDTO);
        return R.ok();
    }


    @Operation(summary = "PC后台管理-提现管理-审核接口", description = "PC后台管理")
    @GetMapping("/profitSharing")
    public R<Void> profitSharing(@RequestParam("orderSn") String orderSn) {
        userWithdrawService.profitSharingByOrderSn(orderSn);
        return R.ok();
    }


}